package com.xsq.newmall01.Controller;

import com.xsq.newmall01.Dao.MerchantUser;
import com.xsq.newmall01.Dao.User;
import com.xsq.newmall01.Service.MerchantUserService.MRegisterService;
import com.xsq.newmall01.Service.UserService.URegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/register")
public class RegisterController {

    // 普通用户注册服务
    @Autowired
    @Qualifier("userregisterservice")
    private URegisterService userURegisterService;

    // 商家用户注册服务
    @Autowired
    @Qualifier("merchantregisterservice")
    private MRegisterService merchantMRegisterService;

    /**
     * 普通用户注册
     */
    @PostMapping("/user")
    public Map<String, Object> registerUser(@RequestBody User user) {
        Map<String, Object> result = new HashMap<>();
        try {
            userURegisterService.register(user);
            result.put("success", true);
            result.put("message", "用户注册成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "注册失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 商家用户注册
     */
    @PostMapping("/merchant")
    public Map<String, Object> registerMerchant(@RequestBody MerchantUser merchantUser) {
        Map<String, Object> result = new HashMap<>();
        try {
            merchantMRegisterService.registerMerchantUser(merchantUser);
            result.put("success", true);
            result.put("message", "商家注册成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "注册失败：" + e.getMessage());
        }
        return result;
    }
}